<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>产品组合审批</title>
    <script type="text/javascript">
      var assemblyOrderAuditWin;
      var assemblyOrderId;
      Ext.onReady(function(){
        var warehouseTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var warehouseStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/warehouse/getAllWarehouse?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });

        var orderNumber = new Ext.form.TextField({
          fieldLabel: '组合单号',
          name: 'order_number',
          width:130
        });

        var productTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var productStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/product/getAllSuitProducts"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });
        //产品
        var productCBB = new Ext.form.ComboBox({
          store: productStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'product_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 25,
          //hideTrigger: false,
          tpl: productTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        var warehouse =  new Ext.form.ComboBox({
          fieldLabel: '组合仓库',
          store: warehouseStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'warehouse_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          width:140,
          //pageSize: 10,
          //hideTrigger: false,
          tpl: warehouseTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction:"all",
          editable:false,
          selectOnFocus:true
        });

        var beginDate = new Ext.form.DateField({
          fieldLabel: '调拨日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            assemblyOrderAuditDS.removeAll();

            assemblyOrderAuditDS.baseParams.order_number = orderNumber.getValue();
            assemblyOrderAuditDS.baseParams.begin_date = beginDate.getRawValue();
            assemblyOrderAuditDS.baseParams.end_date = endDate.getRawValue();
            assemblyOrderAuditDS.baseParams.warehouse_id = warehouse.getValue();
            assemblyOrderAuditDS.baseParams.product_id = productCBB.getValue();
            assemblyOrderAuditDS.load();
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 65,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 900},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .25,
                      layout: 'form',
                      border: false,
                      items: [orderNumber]
                    }, {
                      columnWidth: .25,
                      layout: 'form',
                      border: false,
                      items: [warehouse]
                    }, {
                      columnWidth: .35,
                      layout: 'form',
                      border: false,
                      items: [productCBB]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .25,
                      layout: 'form',
                      border: false,
                      items: [beginDate]
                    }, {
                      columnWidth: .25,
                      layout: 'form',
                      border: false,
                      items: [endDate]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //被选择的订单号。
        var assemblyOrderAuditDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/assembly_order/getAllAssemblyOrdersByAuditStatus'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'order_number'
            }, {
              name: 'action_date'
            }, {
              name: 'description'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            },{
              name: 'department_name',
              mapping:'department.name'
            },{
              name: 'warehouse_name',
              mapping:'warehouse.name'
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_id',
              mapping:'product.id'
            },{
              name: 'quantity',
              mapping:'quantity',
              type: 'int'
            }, {
              name: 'auditing_status'
            }, {
              name: 'audit_opinion'
            },{
              name: 'auditor_name',
              mapping:'auditor',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }])
        });
        
        //生成产品组合Grid的CM
        var assemblyOrderAuditSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var assemblyOrderAuditCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),assemblyOrderAuditSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '组合单号',
              dataIndex: 'order_number',
              width: 110
            },{
              header: '组合仓库',
              dataIndex: 'warehouse_name',
              width: 100
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '编号',
              dataIndex: 'product_number',
              width: 70
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 50
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '组合日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 80
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 50
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 190
            },{
              header: '产品ID',
              dataIndex: 'product_id',
              hidden:true,
              width: 70
            }
          ]});

        //生成要订单的产品的Grid
        var assemblyOrderAuditGrid = new Ext.grid.GridPanel({
          title:'组合订单审批列表',
          store: assemblyOrderAuditDS,
          cm: assemblyOrderAuditCM,
          sm: assemblyOrderAuditSM,
          loadMask: true,
          //renderTo: 'order_assembly_list_grid',
          width: screenWidth-175,
          height: screenHeight-595,
          frame: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: assemblyOrderAuditDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,       
        });

        assemblyOrderAuditGrid.on('rowcontextmenu', function(grid, rowIndex, e){
          assemblyOrderId = assemblyOrderAuditDS.getAt(rowIndex).get('id');
          var auditingStatus = assemblyOrderAuditDS.getAt(rowIndex).get('auditing_status');
          if (auditingStatus=="通过"){
            auditOrder.setDisabled(true);
          }else {
            auditOrder.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditOrder = new Ext.menu.Item({
          text: '【审批产品组合】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        function rightMenuAuditFn(){
          assemblyOrderAuditWin = new Ext.Window({
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '产品组合审批',
            autoLoad: {
              url: "/assembly_order/auditAssemblyOrder?assembly_order_id="+assemblyOrderId,
              scripts: true
            }
          });
          assemblyOrderAuditWin.on("destroy",function(){
            assemblyOrderAuditDS.reload();
          });
          assemblyOrderAuditWin.resizable = false;
          assemblyOrderAuditWin.show();
        }
 
        var rightClick = new Ext.menu.Menu({
          items: [auditOrder]
        });
        
        assemblyOrderAuditDS.load();

        assemblyOrderAuditGrid.on('rowdblclick', function(gridPanel, rowIndex, e){
          assemblyOrderId = assemblyOrderAuditDS.getAt(rowIndex).get('id');
          var assemblyOrderNumber = assemblyOrderAuditDS.getAt(rowIndex).get('order_number');
          //assemblyOrderAuditDS.reload();
          assemblyOrderDetailDS.removeAll();
          assemblyOrderDetailDS.baseParams.assembly_order_id = assemblyOrderId;
          assemblyOrderDetailDS.load();
          assemblyOrderDetailGrid.setTitle("产品组合："+assemblyOrderNumber+"，的详细信息");
        });

        //--------------------------产品组合详细，双击后可显示数据----------------
        //产品组合Grid Store Detail
        var assemblyOrderDetailDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/assembly_order/getAssemblyOrderDetailsById'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_spec',
              mapping:'product.spec'
            },{
              name: 'quantity',
              type: 'int'
            }, {
              name: 'unit_price',
              type: 'float'
            }, {
              name: 'discount',
              type: 'float'
            }, {
              name: 'amount',
              type: 'float'
            }, {
              name: 'description'
            },{
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }])
        });

        //生成产品组合详细表的Grid的CM
        var assemblyOrderDetailSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var assemblyOrderDetailCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),assemblyOrderDetailSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '编号',
              dataIndex: 'product_number',
              width: 70
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 80
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 70
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: Ext.util.Format.numberRenderer('0,000.000'),
              width: 50
            },{
              header: '总价',
              dataIndex: 'amount',
              align: 'right',
              width: 70
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }
          ]});
        //生成要订单的产品的Grid
        var assemblyOrderDetailGrid = new Ext.grid.GridPanel({
          title:'订单详细列表',
          store: assemblyOrderDetailDS,
          cm: assemblyOrderDetailCM,
          sm: assemblyOrderDetailSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-620,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-170,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[queryFormPanel,assemblyOrderAuditGrid,assemblyOrderDetailGrid]
        });
        workbenchPanel.render("assembly_order_audit_list");
      })
    </script>
  </head>
  <body>
    <div id="assembly_order_audit_list"></div>
  </body>
</html>
